package com.cty.service.impl;

import com.cty.mapper.MenuMapper;
import com.cty.mapper.RoleMenuMapper;
import com.cty.pojo.Attributes;
import com.cty.pojo.Menu;
import com.cty.pojo.RoleMenu;
import com.cty.service.MenuService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: cty
 * @Date: 2020/4/1 12:01
 * @Description:
 * @version: 1.0
 */
@Service
public class MenuServiceImpl implements MenuService {
    Logger log = Logger.getLogger(MenuServiceImpl.class);

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<Menu> selAllMenu() {
        List<Menu> menus = this.menuMapper.selByPid(0);  // 查询根菜单
        for(Menu menu:menus){
            List<Menu> children = this.menuMapper.selByPid(menu.getId());
            for(Menu child:children){  // 为每一个子菜单添加Attributes属性
                Attributes attr = new Attributes();
                attr.setFilename(child.getFilename());
                child.setAttributes(attr);
            }
            menu.setChildren(children);
        }
        log.info("查询菜单："+menus);
        return menus;
    }

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> selAllAddChecked(int rid) {
        List<RoleMenu> roleMenus = this.roleMenuMapper.selByRid(rid);

        List<Menu> menus = this.menuMapper.selByPid(0);  // 查询根菜单
        for(Menu menu:menus){
            List<Menu> children = this.menuMapper.selByPid(menu.getId());
            for(Menu child:children){  // 此时不需要Attributes属性，需要checked属性
                for(RoleMenu roleMenu:roleMenus){
                    if(child.getId()==roleMenu.getMid()){
                        child.setChecked(true);
                    }
                }
            }
            menu.setChildren(children);
        }
        return menus;
    }
}
